﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;

namespace com.huigo.B2B.Web.center.order
{
    public partial class OrderList : BasePage
    {
        /// <summary>
        /// 订单号
        /// </summary>
        private string queryOrderNo
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["orderNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["orderNo"]);
            }
        }
        /// <summary>
        /// 订单开始时间
        /// </summary>
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.MinValue;
            }
        }
        /// <summary>
        /// 订单结束时间
        /// </summary>
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.MinValue;
            }
        }
        /// <summary>
        /// 订单状态
        /// </summary>
        private int queryOrderType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["orderType"]) ? int.MinValue : Convert.ToUInt16(com.loto.Common.Utils.SqlEscape(Request.QueryString["orderType"]));
            }
        }
        /// <summary>
        /// 保险公司
        /// </summary>
        private long queryInsCompany
        {
            get
            {
                return com.loto.Common.Utils.IsNumeric(Request.QueryString["insCompany"]) ? Convert.ToInt64(com.loto.Common.Utils.SqlEscape(Request.QueryString["insCompany"])) : long.MinValue;
            }
        }
        /// <summary>
        /// 保险类型
        /// </summary>
        private long queryType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["Type"]) ? long.MinValue : Convert.ToInt64(com.loto.Common.Utils.SqlEscape(Request.QueryString["Type"]));
            }
        }
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        public string Notiy = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                DropSelected();
                BindData();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "OrderList.aspx?";
            url += "page=1";
            if (policyName.Text != string.Empty)
                url += "&orderNo=" + policyName.Text;
            if (beginDate.Text != string.Empty)
                url += "&Start=" + beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&End=" + endDate.Text;
            if (proType.SelectedValue != string.Empty)
                url += "&Type=" + proType.SelectedValue;
            if (insList.SelectedItem.Text != string.Empty)
                url += "&insCompany=" + insList.SelectedValue;
            if (statusList.SelectedValue != string.Empty)
                url += "&orderType=" + statusList.SelectedValue;
            Response.Redirect(url);
        }
        /// <summary>
        /// Dropdownlist选中
        /// </summary>
        private void DropSelected()
        {
            if (statusList.Items.Count > 0)
            {
                for (int i = 0; i < statusList.Items.Count; i++)
                {
                    if (statusList.Items[i].Value == queryOrderType.ToString())
                    {
                        statusList.SelectedIndex = i;
                    }
                }
            }
            if (insList.Items.Count > 0)
            {
                for (int i = 0; i < insList.Items.Count; i++)
                {
                    if (insList.Items[i].Value == queryInsCompany.ToString())
                    {
                        insList.SelectedIndex = i;
                    }
                }
            }
            if (proType.Items.Count > 0)
            {
                for (int i = 0; i < proType.Items.Count; i++)
                {
                    if (proType.Items[i].Value == queryType.ToString())
                    {
                        proType.SelectedIndex = i;
                    }
                }
            }
        }
        /// <summary>
        /// 订单数据绑定
        /// </summary>
        private void BindData()
        {
            if (!string.IsNullOrEmpty(queryOrderNo))
            {
                policyName.Text = queryOrderNo;
            }
            if (queryStart != DateTime.MinValue)
            {
                beginDate.Text = Convert.ToDateTime(queryStart).ToString("yyyy-MM-dd");
            }
            if (queryEnd != DateTime.MinValue)
            {
                endDate.Text = Convert.ToDateTime(queryEnd).ToString("yyyy-MM-dd");
            }
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("orderNO", queryOrderNo));
            argList.Add(new RequestFuncParams("orderStart", queryStart.ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("orderEnd", queryEnd.AddDays(1).ToString("yyyy-MM-dd")));
            argList.Add(new RequestFuncParams("orderType", queryOrderType.ToString()));
            argList.Add(new RequestFuncParams("insCompanyId", queryInsCompany.ToString()));
            argList.Add(new RequestFuncParams("proType", queryType.ToString()));
            argList.Add(new RequestFuncParams("custFid", CustId.ToString()));
            argList.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            argList.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.GET_ORDER_INFO_LIST, argList);
            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                OrderListContent.DataSource = parser.ModelList;
                OrderListContent.DataBind();
            }
            else
            {
                Notiy = " <table class=\"table table-bordered table-striped responsive-utilities table-hover\"><thead><tr><th>订单号</th><th>保险名称</th><th>被保人数</th><th>订单金额</th><th>订单时间</th><th>订单状态</th></tr></thead><tbody><tr><td colspan=\"6\" style=\"text-align:center;\">没有相关信息</td></tr></tbody></table>";
            }
        }
        /// <summary>
        /// Dropdownlist数据绑定
        /// </summary>
        private void LoadData()
        {
            //订单状态
            List<DropList> statusDropList = Tools.DictToDropList(Dict.DictOrderStatus);
            if (statusDropList != null)
            {
                statusList.DataSource = statusDropList;
                statusList.DataValueField = "Id";
                statusList.DataTextField = "Value";
                statusList.DataBind();
            };

            //保险公司列表
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            BaseService baseService = new BaseService();
            ParserXml parserXml = baseService.RequestEntryService(Dict.ERequestFunc.GET_INS_COMPANY_DROPLIST, argList);

            List<DropList> cDropList = Tools.ParserToDropList(parserXml, "CompanyId", "CompanyShortName");
            if (cDropList != null)
            {
                insList.DataSource = cDropList;
                insList.DataValueField = "Id";
                insList.DataTextField = "Value";
                insList.DataBind();
            }

            //保单类型
            List<DropList> tDropList = Tools.DictToDropList(Dict.DictInsType);
            if (tDropList != null)
            {
                proType.DataSource = tDropList;
                proType.DataValueField = "Id";
                proType.DataTextField = "Value";
                proType.DataBind();
            };
        }
        private string GetPagerUrl()
        {
            string url = "OrderList.aspx?";
            url += "page={0}";
            if (queryOrderNo != string.Empty)
                url += "&orderNo=" + queryOrderNo;
            if (queryStart != DateTime.MinValue)
                url += "&Start=" + queryStart;
            if (queryEnd != DateTime.MinValue)
                url += "&End=" + queryEnd;
            if (queryType != long.MinValue)
                url += "&Type=" + queryType;
            if (queryInsCompany != long.MinValue)
                url += "&insCompany=" + queryInsCompany;
            if (queryOrderType != int.MinValue)
                url += "&orderType=" + queryOrderType;
            return url;
        }
    }
}
